package sz.com.postoffice.jxyzwgcase.service.impl;

import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.hibernate.validator.cfg.defs.AssertFalseDef;
import org.springframework.stereotype.Service;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.dao.master.EmolumentResultDao;
import sz.com.postoffice.jxyzwgcase.pojo.EmolumentResult;
import sz.com.postoffice.jxyzwgcase.service.EmolumentResultService;
import sz.com.postoffice.jxyzwgcase.service.EmolumentRuleService;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;


/**
 * @ClassName EmolumentRuleServiceImpl
 * @Deacription 薪酬结果处理层
 * @Author jmx
 * @Date 2020/12/6 18:00
 * @Version 1.0
 **/
@Service
@Slf4j
public class EmolumentResultServiceImpl extends BaseServiceImpl<EmolumentResult> implements EmolumentResultService {

    @Resource
    private EmolumentResultDao emolumentResultDao;

    @Override
    protected BaseDao<EmolumentResult> getMapper() {
        return this.emolumentResultDao;
    }

    @Override
    public List<EmolumentResult> getTotalByDeptAndPeriod(String deptCode, String periodId) {
        return emolumentResultDao.getTotalByDeptAndPeriod(deptCode, periodId);
    }

    @Override
    public XSSFWorkbook exportResults(EmolumentResult paramResult) {
        //查询数据
        TableDataInfo byListEntity = this.findByListEntity(paramResult);
        List<EmolumentResult> results = (List<EmolumentResult>) byListEntity.getRows();
        // 第一步：定义一个新的工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        // 第二步：创建一个Sheet页
        XSSFSheet sheet = wb.createSheet("薪酬汇总");
        sheet.setDefaultRowHeight((short) (2 * 256));//设置行高
        sheet.setColumnWidth(0, 4000);//设置列宽
        sheet.setColumnWidth(1, 4000);
        sheet.setColumnWidth(2, 4000);
        sheet.setColumnWidth(3, 16000);
        sheet.setColumnWidth(4, 4000);
        sheet.setColumnWidth(5, 4000);
        sheet.setColumnWidth(6, 4000);
        sheet.setColumnWidth(7, 4000);
        sheet.setColumnWidth(8, 4000);
        sheet.setColumnWidth(9, 4000);
        sheet.setColumnWidth(10, 4000);
        sheet.setColumnWidth(11, 4000);
        XSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);

        //创建查询的添加的行
        XSSFCellStyle cellStyle1 = (XSSFCellStyle) wb.createCellStyle();
        cellStyle1.setAlignment(HorizontalAlignment.CENTER);
        //表头
        Assert.notBlank(paramResult.getPeriodId(), "查询月份的参数不能为空！！");
        String year = paramResult.getPeriodId().substring(0, 4);
        String month = paramResult.getPeriodId().substring(4);
        String Value = year + "-" + month + "薪酬汇总";

        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = null;
        CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 11);
        sheet.addMergedRegion(cellRangeAddress);
        cell = row.createCell(0);
        cell.setCellStyle(cellStyle1);
        cell.setCellValue(Value);
        //写入菜单栏
        XSSFRow row2 = sheet.createRow(1);
        String[] taskTitles = {"省", "州市", "区县", "营业部", "工号", "姓名", "总薪酬", "底薪", "投递提成收入", "标快提成收入", "快递包裹提成收入", "国际快递提成收入"};
        for (int i = 0; i < taskTitles.length; i++) {
            cell = row2.createCell(i);
            cell.setCellValue(taskTitles[i]);
            cell.setCellStyle(cellStyle1);
        }
        XSSFRow rows;
        XSSFCell cells;
        for (int i = 0; i < results.size(); i++) {
            rows = sheet.createRow(i + 2);
            cells = rows.createCell(0);
            cells.setCellValue(results.get(i).getProvinceName());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(1);
            cells.setCellValue(results.get(i).getCityName());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(2);
            cells.setCellValue(results.get(i).getCountyName());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(3);
            cells.setCellValue(results.get(i).getDeptName());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(4);
            cells.setCellValue(results.get(i).getEmpCode());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(5);
            cells.setCellValue(results.get(i).getEmpName());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(6);
            cells.setCellValue(results.get(i).getTotal());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(7);
            cells.setCellValue(results.get(i).getBasicSalary());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(8);
            cells.setCellValue(results.get(i).getDeliverCourtTotal());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(9);
            cells.setCellValue(results.get(i).getStandardDeliveryTotal());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(10);
            cells.setCellValue(results.get(i).getExpressPackageTotal());
            cells.setCellStyle(cellStyle1);
            cells = rows.createCell(11);
            cells.setCellValue(results.get(i).getInternationalDeliveryTotal());
            cells.setCellStyle(cellStyle1);

        }
        return wb;
    }
}
